package com.atzxh.guigu.juc.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 小红同学
 * @create 2023-04-28-17:13
 * @description     自定义线程池演示
 */
public class CustomThreadPoolDemo {
    public static void main(String[] args) {
        /**
         * int corePoolSize,    常驻（核心）线程数量
         * int maximumPoolSize,  最大线程数量
         * long keepAliveTime,  最长等待时间
         * TimeUnit unit,   设置时间单位
         * BlockingQueue<Runnable> workQueue,  阻塞队列
         * ThreadFactory threadFactory,    线程工厂
         * RejectedExecutionHandler handler     拒绝策略
         */
        ThreadPoolExecutor customThreadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        // 有10个顾客请求
        try {
            for (int i = 1; i <= 100; i++) {
                // 执行任务
                customThreadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName() + "  正在办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            customThreadPool.shutdown();
        }
    }
}
